package com.liu.common.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.liu.common.content.CommonInterface;
import com.liu.qqRobot.wiki.entity.dto.WikiSearchDTO;
import com.liu.qqRobot.wiki.entity.dto.WikiSearchResultDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Slf4j
public class WikiClientUtils {

    /**
     * wiki关键词查询
     * @param search 关键词
     * @return WikiSearchDTO
     */
    public static WikiSearchDTO search(String search) {
        WikiSearchDTO result = new WikiSearchDTO();
        result.setSearch(search);
        String url = CommonInterface.HUI_JI_WIKI_URL_HEAD
                + "/api.php?action=opensearch&format=json&search="
                + URLUtil.encode(search);
        String body = HttpRequest.post(url)
                .header("Connection", "keep-alive")
                .header("Cookie", "__cf_bm=P3EVOy0oJDVvAzNFexIbPQRgp6HzkwqLDpPiwLPMUMg-1723858418-1.0.1.1-rBjEGbQcS.198Y8P8J.n88lQtYQ9cx5GF8qq1.eKZo9EjUNG6VFAcnhnQFA59yhb15l0n8oOFLx002.UJTUzhQ")
                .timeout(60000)
                .execute()
                .body();
        JSONArray objects = JSONUtil.parseArray(body);
        if (CollUtil.isEmpty((Collection<?>) objects.get(1))) {
            return null;
        }
        List<WikiSearchResultDTO> wikiSearchResultDTOS = new ArrayList<>();
        result.setWikiSearchResultDTOS(wikiSearchResultDTOS);
        // 匹配词、简介、链接顺序一致
        for (int i = 0; i < objects.size(); i++) {
            WikiSearchResultDTO wikiSearchResultDTO;
            // 搜索词
            if (i == 0) {
                Object o = objects.get(i);
                search = URLUtil.decode((String) o);
            }
            // 搜索词匹配词
            if (i == 1) {
                Object o = objects.get(i);
                JSONArray nameJsonArray = JSONUtil.parseArray(o);
                for (int i1 = 0; i1 < nameJsonArray.size(); i1++) {
                    wikiSearchResultDTO = new WikiSearchResultDTO();
                    wikiSearchResultDTO.setName(URLUtil.decode((String) nameJsonArray.get(i1)));
                    wikiSearchResultDTOS.add(wikiSearchResultDTO);
                }
            }
            // 匹配词简介
            if (i == 2) {
                Object o = objects.get(i);
                JSONArray nameJsonArray = JSONUtil.parseArray(o);
                for (int i1 = 0; i1 < nameJsonArray.size(); i1++) {
                    for (int i2 = 0; i2 < wikiSearchResultDTOS.size(); i2++) {
                        if (i1 == i2) {
                            wikiSearchResultDTO = wikiSearchResultDTOS.get(i2);
                            wikiSearchResultDTO.setBriefIntroduction(URLUtil.decode((String) nameJsonArray.get(i1)));
                        }
                    }
                }
            }
            // 匹配词链接
            if (i == 3) {
                Object o = objects.get(i);
                JSONArray nameJsonArray = JSONUtil.parseArray(o);
                for (int i1 = 0; i1 < nameJsonArray.size(); i1++) {
                    for (int i2 = 0; i2 < wikiSearchResultDTOS.size(); i2++) {
                        if (i1 == i2) {
                            wikiSearchResultDTO = wikiSearchResultDTOS.get(i2);
                            wikiSearchResultDTO.setUrl((String) nameJsonArray.get(i1));
                        }
                    }
                }
            }
        }
        return result;
    }
}
